<?php
/**
 * workflow
 * 工作流
 * @author Administrator
 * @version
 */
class Model_Tables_Workflow extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'workflow';
    /**
     * add
     * 增加工作流
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'name' => $params['name'],
		'description' => $params['description'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑工作流
     * @param $params 字段信息
     * @param $workflowid 主键值
     */
    public function modify( $params, $workflowid )
    {
        $data = array(
		'name' => $params['name'],
		'description' => $params['description'],
        );
        $where = 'workflowid = '.$workflowid ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑工作流
     * @param $workflowid 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $workflowid, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("workflowid=?", $workflowid);
    	return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转工作流
     * @param $field 需要反转的字段
     * @param $workflowid 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update workflow set  $field =not  $field  where workflowid=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除工作流
     * @param $workflowid 主键值
     */
    public function del( $workflowid )
    {
//     	$where = 'workflowid = '.$workflowid;
//        return $this->delete($where);
        $db = $this->getAdapter();
    	$db->beginTransaction();
		try {
			$where = $db->quoteInto('workflowid = ?',$workflowid);
        	$this->delete($where);
        	$processsql = $db->quoteInto("delete from process where workflowid =?",$workflowid);
		    $db->query($processsql);
		   
		    $db->commit();
		} catch (Exception $e) {
		    $db->rollBack();
		    return false;
		}
		return true;
    }
    /**
     * getInfo
     * 获取工作流信息
     * @param $workflowid 主键值
     * @return 结果数组
     */
    public function getInfo($workflowid)
    {
        $row = $this->fetchRow('workflowid = ' . $workflowid);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取工作流数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("name like ?", "%$s%");
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取工作流数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('workflowid');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("name like ?", "%$s%");
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    public  function getWorkflowList(){
    	$result0 = $this->getList(1,100);
    	$result = array();
    	if (!empty($result0))
    	{
    		foreach ($result0 as $item)
    		{
    			$result[$item['workflowid']] = $item['name'];
    		}
    	}
    	return $result;
    }
}